package com.ruoyi.wms.service;

import com.ruoyi.common.core.utils.MapstructUtils;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.wms.domain.bo.stock.ConversionRulesBo;
import com.ruoyi.wms.domain.vo.ConversionRulesVo;
import com.ruoyi.wms.domain.entity.stock.ConversionRules;
import com.ruoyi.wms.mapper.ConversionRulesMapper;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 转换关系表Service业务层处理
 *
 * @author 邓滔滔
 * @date 2025-01-15
 */
@RequiredArgsConstructor
@Service
public class ConversionRulesService {

    private final ConversionRulesMapper conversionRulesMapper;

    /**
     * 查询转换关系表
     */
    public ConversionRulesVo queryById(Long ruleId){
        return conversionRulesMapper.selectVoById(ruleId);
    }

    /**
     * 查询转换关系表列表
     */
    public TableDataInfo<ConversionRulesVo> queryPageList(ConversionRulesBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ConversionRules> lqw = buildQueryWrapper(bo);
        Page<ConversionRulesVo> result = conversionRulesMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询转换关系表列表
     */
    public List<ConversionRulesVo> queryList(ConversionRulesBo bo) {
        LambdaQueryWrapper<ConversionRules> lqw = buildQueryWrapper(bo);
        return conversionRulesMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ConversionRules> buildQueryWrapper(ConversionRulesBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ConversionRules> lqw = Wrappers.lambdaQuery();
        lqw.between(params.get("beginUpdateTime") != null && params.get("endUpdateTime") != null,
            ConversionRules::getUpdateTime ,params.get("beginUpdateTime"), params.get("endUpdateTime"));
        return lqw;
    }

    /**
     * 新增转换关系表
     */
    public void insertByBo(ConversionRulesBo bo) {
        Long skuId = bo.getSkuId();
        Long targetSkuId = bo.getTargetSkuId();
        //这两个不能为相同，相同则报错
        if (skuId.equals(targetSkuId)) {
            throw new RuntimeException("规格不能相同");
        }
        ConversionRules add = MapstructUtils.convert(bo, ConversionRules.class);
        conversionRulesMapper.insert(add);
    }

    /**
     * 修改转换关系表
     */
    public void updateByBo(ConversionRulesBo bo) {
        Long skuId = bo.getSkuId();
        Long targetSkuId = bo.getTargetSkuId();
        //这两个不能为相同，相同则报错
        if (skuId.equals(targetSkuId)) {
            throw new RuntimeException("规格不能相同");
        }
        ConversionRules update = MapstructUtils.convert(bo, ConversionRules.class);
        conversionRulesMapper.updateById(update);
    }

    /**
     * 批量删除转换关系表
     */
    public void deleteByIds(Collection<Long> ids) {
        conversionRulesMapper.deleteBatchIds(ids);
    }
}
